/**
 * 定义公共变量，记录cid的值
 * @type {string}
 */
var cid="";

/**
 * 定义页码
 */
var pageNum=1;

var orderNum=0;


$(function(){
    //条件查询开始
    $("#distpicker1").distpicker({
        autoSelect: false
    });

    var datas = [
        {
            name:'律师姓名',
            className:'.lawyer-name',
            parameter:''
        },
        {
            name:'手机号码',
            className:'.phone-num',
            parameter:''
        },
        {
            name:'客户服务数start',
            className:'.start-num',
            parameter:''
        },
        {
            name:'客户服务数end',
            className:'.end-num',
            parameter:''
        },
        {
            name:'创建时间开始',
            className:'#create-start',
            parameter:''
        },
        {
            name:'创建时间结束',
            className:'#create-end',
            parameter:''
        }
    ]
    //获取搜索条件
    $('.seek-btn').on('click',function(){
        var provinceId = $('.province option:selected').attr('data-code');
        var cityId = $('.city option:selected').attr('data-code');
        var districtId = $('.district option:selected').attr('data-code');

        //获取擅长领域里选中的checkbox
        $('.goodat').find('input:checked').each(function(m,n){
          //  preferFieldList.push(m);
        })
        //获取服务平台选中checkbox
        $('.service').find('input:checked').each(function(m,n){
            console.log(m,n);
        })
        //发送ajax;
        cid="";


        if(provinceId!=""){

            cid=provinceId;

        }

        if(cityId!=""){

            cid=cityId;

        }


        if(districtId!=""){

            cid=districtId;

        }


        /**
         * 每一次搜索，都把页码回归成第一页
         * @type {number}
         */
        pageNum=1;

        lawyer_list()

    })
    //清空条件
    $('.seek-clear').on('click',function(){
        for(var i = 0 ; i<datas.length;i++){
            var data = datas[i];
            $(data.className).val('');
        }
        $('.province option:eq(0)').prop('selected',true)
        $('.city option:eq(0)').prop('selected',true)
        $('.district option:eq(0)').prop('selected',true)

        $("#myForm")[0].reset();
        cid='';
    })
    //导出xls
    $('.seek-xls').on('click',function(){

        var selefrom = $("#myForm").serialize();

        var json = getJson();
        location.href="/lawyer/exportExcel?"+selefrom+ (cid!=''?'&cid='+cid:'');

    })

    //条件查询结束
    //表格部分开始

    //表格头部箭头向上 时间最近

    $('.create-time').on('click',function(){

        if ( $(this).hasClass('isup')){
            orderNum=0;
            $(this).removeClass('isup').addClass('isdown');

        }else{
            orderNum=1;
            $(this).removeClass('isdown').addClass('isup');
        }

        lawyer_list();

    })



    $('.service-client-num').on('click',function(){

        if ( $(this).hasClass('isup')){

            orderNum=2;

            $(this).removeClass('isup').addClass('isdown');

        } else{
            orderNum=3;
            $(this).removeClass('isdown').addClass('isup');
        }

        lawyer_list();
    })

    //全选按钮

    $('#check-all').on('click',function(){
        $('.check-unit').prop('checked',$(this).prop('checked'))
    })
    //批量冻结弹框开始
    $('.freeze').on('click',function(){
        var num =  util.getCheck($('.check-unit')).length;
        if(num == 0 ){
            alert('请先选择要冻结的用户');
            return;
        }
        $('.freeze-quesition').html('确认要冻结律师账户吗？');
        $('.freeze-info').html('冻结后该律师将不能接到客户咨询、无法提现及更改个 人资料等。');
        $('#freeze-info').cmsmodal('show');

        //确定
        $('.freeze-confirm').off('click').on('click',function(){
            $('#freeze-info').cmsmodal('hide');

            //批量冻结

            batch_freeze(0);

        })
        //取消
        $('.freeze-cancel').off('click').on('click',function(){
            $('#freeze-info').cmsmodal('hide');
        })
    })

    //批量冻结弹框结束

    //批量解冻弹框开始
    $('.unfreeze').on('click',function(){
        var num =  util.getCheck($('.check-unit')).length;
        if(num == 0 ){
            alert('请先选择要解冻的用户');
            return;
        }
        $('.freeze-quesition').html('确认要将律师账户恢复正常吗？');
        $('.freeze-info').html('解冻后该律师账户功能将恢复正常。');
        $('#freeze-info').cmsmodal('show');

        //确定
        $('.freeze-confirm').off('click').on('click',function(){
            $('#freeze-info').cmsmodal('hide');
            //批量解冻

            batch_freeze(1);
        })
        //取消
        $('.freeze-cancel').off('click').on('click',function(){
            $('#freeze-info').cmsmodal('hide');
        })
    })
    //上下线弹框开始
    $('.table-box').on('click','.common-thread',function(){


        var lawyer_id=$(this).attr("data-value");

        var on_off_line=$(this).hasClass('up-thread');

        if(on_off_line){
            $('.thread-quesition').html('确定要将该律师上线吗');
        }else if($(this).hasClass('down-thread')){
            $('.thread-quesition').html('确定要将该律师下线吗');
        }
        $('#thread').cmsmodal('show');


        //确定
        $('.thread-confirm').off('click').on('click',function(){
            $('#thread').cmsmodal('hide');

            /**
             * 批量处理上线下线的功能
             */

            off_line(on_off_line?1:0,lawyer_id);


        })
        //取消
        $('.thread-cancel').off('click').on('click',function(){
            $('#thread').cmsmodal('hide');
        })
    })

    //上下线弹框结束



//   表格部分结束
    lawyer_list();

})


/**
 * 打开编辑页面
 */
function openEdit(self){

    var lawyer_id=$(self).attr("data-value");

    //加载编辑律师编辑数据
    edit_load(lawyer_id);
    $('#edit-material').cmsmodal('show');

}

/**
 *加载律师
 */
 function  lawyer_list(){

     //Loading.show();


    $("#check-all").removeAttr("checked");

     var selefrom = $("#myForm").serialize();

     $.ajax({
        type:"post",
        url:"/lawyer/getLawyerList",
        data:selefrom+(cid!=''?'&cid='+cid:'')+"&pageNum="+pageNum+"&orderNum="+orderNum,
        dataType: "json",
        success:function(data){
            $("#currentCount").text('律师数量：'+data.data.total);
            console.log("律师数量："+data.data.total);
            v_data(function(){

               var tmp=   bt('list_temp', data.data);
                document.getElementById('list').innerHTML = tmp;
                  //颜色
                var lawyerStatess = $('.lawyerStates');
                for (i = 0; i < lawyerStatess.length; i++) {
                    var tr = $(lawyerStatess[i]).parent("tr");
                    var v = lawyerStatess[i].innerHTML;
                    if(v=='上线'){
                        $(lawyerStatess[i]).parent("tr").find(".lawyerStates").css("color", "green");
                    }else{
                        $(lawyerStatess[i]).parent("tr").find(".lawyerStates").css("color", "red");
                    }
                }


            },lawyer_list,data.data.result.length,data.data.total,'list');



        },
        error:function(){
            alert("报错啦");
        }
     })
   }

/**
 * 列表排序
 * @param type
 */
    function order_by(type){


        if(type){

            $("#isValid_hidden").val($('#isValid option:selected').val());

        }else{

            $("#lawyerStates_hidden").val($('#lawyerStates option:selected').val());

        }

        lawyer_list();

    }


/**
 * 批量冻结和批量解冻的方法
 * @param type 0，代表冻结，1代表解冻
 */
    function batch_freeze(type){


        Loading.show();


        var lawyerIdList=new Array();

        $('.check-unit:checked').each(function(m,n){

            lawyerIdList.push($(n).val());

        })

        $.ajax({
            type:"post",
            url:"/lawyer/"+(type==0?'frozeLawyerAccount':'releaseFrozeLawyer'),
            data:{'lawyerIdList':lawyerIdList},
            dataType: "json",
            success:function(data){

                 if(data.code=="1"){

                     alert(data.msg);

                     lawyer_list();

                 }else{

                     alert(data.msg);

                 }


                Loading.hide();

            },
            error:function(){
                alert("报错啦");
            }
        })

    }


    /**
     * 处理上线下线的问题
     * @param type
     */
    function off_line(type,lawyer_id){


        Loading.show();

        $.ajax({
            type:"get",
            url:"/lawyer/"+(type==0?'updateOffline':'updateOnline'),
            data:{lawyerId:lawyer_id},
            dataType: "json",
            success:function(data){

                if(data.code=="1"){

                    alert(data.msg);

                    lawyer_list();

                }else{

                    alert(data.msg);

                }


                Loading.hide();

            },
            error:function(){
                alert("报错啦");
            }
        })

    }

    /**
     * 条件判断
      * @param is_valid
     * @returns {*}
     */
    function back_tmpl(is_valid){

            if(is_valid==2){ return '冻结';}else if(is_valid==1){ return '正常'}else{ return '待审核'}
     }


    /**
     * 打开编辑页面，加载个人信息
     * @param type
     */
    function edit_load(lawyer_id){

        Loading.show();

        $.ajax({
            type:"post",
            url:"/lawyer/findLawyerDetailInfoEdit",
            data:{lawyerid:lawyer_id},
            dataType: "json",
            success:function(data){

                console.log(data.data);

                setHtml('lawyer_temp_div',bt('lawyer_temp', data.data));

                Loading.hide();

            },
            error:function(){
                alert("报错啦");
            }
        })

    }


    /*
    *添加按钮，插入一个输入框
     */
    function add_input(){
        $('#s_h_r_y').append($('<div><input type="text" maxlength="10" name="add_honor" class="form-control"/></div>'));
    }


    /**
     * 编辑信息保存
     */
    function edit_save(lawyer_id) {

        Loading.show();

        var isValid = $("#edit_is_valid").val();

        var editPlates = new Array();

        var addHonor = new Array();

        var editAdvantage=new Array();


        $("input[name='edit_plate']:checked").each(function (m, n) {

            editPlates.push($(n).val());

        })


        var editHonor = {};

        $("input[name='edit_honor']").each(function (m, n) {


            var v_v = $(n).val();

            var k_v = $(n).attr("data-value");

            editHonor[k_v] = v_v;


        })

        $("input[name='add_honor']").each(function (m, n) {


            var val = $(n).val().trim();

            if (val != "") {

                addHonor.push(val);
            }


        });

        $(".edit_advantage option:selected").each(function (m, n) {

            editAdvantage.push($(n).val());

        })



        $.ajax({
            type: "post",
            url: "/lawyer/updateLwayerInfo",
            data: {
                editPlates: editPlates,
                editAdvantage:editAdvantage,
                addHonor: addHonor,
                editHonor: JSON.stringify(editHonor),
                lawyerId: lawyer_id,
                isValid: isValid
            },
            dataType: "json",
            success: function (data) {


                if (data.code == "1") {

                    alert(data.msg);

                    lawyer_list();

                } else {

                    alert(data.msg);

                }

                $('#edit-material').cmsmodal('hide');

                Loading.hide();

            },
            error: function () {
                alert("报错啦");
            }
        })

    }

/**
 * 保存编辑数据
 */
function save_edit_data(self){


    var lawyer_id=$(self).attr("data-value");

    edit_save(lawyer_id);

}

